package com.project.controller.web;

import com.project.entity.Cv;
import com.project.entity.School;
import com.project.myBeans.ResponseBean;
import com.project.service.CvService;
import com.project.utils.FileUtil;
import com.project.utils.Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.List;

/**
 * 简历控制器
 */
@Controller
@RequestMapping("/cv")
public class CvController {

    @Autowired
    private CvService cvService;

    //根目录
    private static final String basePath = "/WEB-INF/static/upload";

    @RequestMapping(value = "/reName/{cvId}/{name}", method = RequestMethod.GET)
    @ResponseBody
    /**
     * author: WheelChen
     * date: 2017/9/5
     * function: 修改文件名
     *
     * @param name 待修改文件名
     * @param cvId 简历id号
     * @param request
     * @return
     */
    public ResponseBean reName(@PathVariable("name") String name, @PathVariable("cvId") Integer cvId, HttpServletRequest request) {
        ResponseBean responseBean = new ResponseBean();
        try {
            Cv cv = cvService.findOneById(cvId);
            //cv目录的绝对地址
            String cvAbsolutePath = request.getSession().getServletContext().getRealPath(basePath);
            //cv目录下的细分目录
            String belong = cv.getFilename().substring(0, cv.getFilename().lastIndexOf("/"));
            //完整地址
            String realPath = cvAbsolutePath + "/" + belong;
            //old文件名
            String oldFileName = cv.getFilename().substring(cv.getFilename().lastIndexOf("/") + 1, cv.getFilename().length());
            //旧文件
            File oldFile = new File(realPath + "/" + oldFileName);
            //获取文件后缀
            String ext = Util.getExt(cv.getFilename());

            name +=  "." + ext;

            File newFile = new File(realPath + "/" + name);
            if (!oldFile.exists()) {
                responseBean.setSuccess(false);
                responseBean.setMessage("待修改文件不存在");
            } else if (newFile.exists()) {
                responseBean.setSuccess(false);
                responseBean.setMessage("该名称已存在");
            } else {
                oldFile.renameTo(newFile);
                cv.setFilename(belong + "/" + name);
                cvService.modifyOneById(cv);
                responseBean.setSuccess(true);
                responseBean.setMessage("修改成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
            responseBean.setSuccess(false);
            responseBean.setMessage("程序异常");
        }
        return responseBean;
    }

    @RequestMapping(value = "/delOne/{id}", method = RequestMethod.GET)
    @ResponseBody
    /**
     * author: WheelChen
     * date: 2017/9/5
     * function: 删除单个文件
     *
     * @param id
     * @return
     */
    public ResponseBean delOne(@PathVariable("id") Integer id, HttpServletRequest request) {
        ResponseBean responseBean = new ResponseBean();
        try {
            Cv cv = cvService.findOneById(id);
            //简历的基本目录的绝对地址
            String absolutePath = request.getSession().getServletContext().getRealPath(basePath);

            String realPath = absolutePath + "/" + cv.getFilename();

            if (cvService.removeOneById(id,realPath)) {
                responseBean.setSuccess(true);
                responseBean.setMessage("删除成功");
            } else {
                responseBean.setSuccess(false);
                responseBean.setMessage("删除失败");
            }

        } catch (Exception e) {
            e.printStackTrace();
            responseBean.setSuccess(false);
            responseBean.setMessage("程序异常");
        }
        return responseBean;
    }

    @RequestMapping(value = "/findByCondition", method = RequestMethod.POST)
    @ResponseBody
    /**
     * author: WheelChen
     * date: 2017/9/7
     * function: 根据条件模糊查找简历
     *
     * @param cv
     * @return
     */
    public ResponseBean findByCondition(@RequestBody Cv cv) {
        ResponseBean responseBean = new ResponseBean();
        try {
            if (cv.getPageIndex() > 0) cv.setPageIndex(cv.getPageIndex() - 1);
            responseBean.setData(cvService.findByCondition(cv));
            responseBean.setSuccess(true);
            responseBean.setTotal(cv.getTotal());
            responseBean.setMessage("获取简历列表成功");
        } catch (Exception e) {
            e.printStackTrace();
            responseBean.setSuccess(false);
            responseBean.setMessage("程序异常");
        }
        return responseBean;
    }

    @RequestMapping(value = "/modifyOneById", method = RequestMethod.POST)
    @ResponseBody
    /**
     * author: WheelChen
     * date: 2017/9/8
     * function: 根据id修改简历
     *
     * @param cv 简历表id
     * @return
     */
    public ResponseBean modifyOneById(@RequestBody Cv cv){
        ResponseBean responseBean = new ResponseBean();
        try {
            cvService.modifyOneById(cv);
            responseBean.setSuccess(true);
            responseBean.setMessage("修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            responseBean.setSuccess(false);
            responseBean.setMessage("程序异常");
        }
        return responseBean;
    }

    @RequestMapping(value = "/delBatchByStid/{stid}", method = RequestMethod.GET)
    @ResponseBody
    /**
     * author: WheelChen
     * date: 2017/9/30
     * function: 根据学生id批量删除学生简历
     *
     * @param stid
     * @param request
     * @return
     */
    public ResponseBean delBatchByStid(@PathVariable("stid") Integer stid,  HttpServletRequest request){
        ResponseBean responseBean = new ResponseBean();
        try {
            //简历的基本目录的绝对地址
            String absolutePath = request.getSession().getServletContext().getRealPath(basePath);
            List<Cv> cvList = cvService.findBatchByStid(stid);
            for (Cv cv : cvList){
                String realPath = absolutePath + "/" + cv.getFilename();
                cvService.removeOneById(cv.getId(), realPath);
            }
            responseBean.setSuccess(true);
            responseBean.setMessage("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            responseBean.setSuccess(false);
            responseBean.setMessage("程序异常");
        }
        return responseBean;
    }

    @RequestMapping(value = "/findOneById/{id}", method = RequestMethod.GET)
    @ResponseBody
    /**
     * author: WheelChen
     * date: 2017/10/8
     * function: 根据id查找
     *
     * @param id
     * @return
     */
    public ResponseBean findOneById(@PathVariable("id") Integer id){
        ResponseBean responseBean = new ResponseBean();
        try {
            responseBean.setData(cvService.findOneById(id));
            responseBean.setSuccess(true);
            responseBean.setMessage("查找成功");
        } catch (Exception e) {
            e.printStackTrace();
            responseBean.setSuccess(false);
            responseBean.setMessage("程序异常");
        }
        return responseBean;
    }


}
